package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.Result;
import com.example.springboot.entity.TUser;
import com.example.springboot.service.ITUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lml
 * @since 2023-01-08
 */
@RestController
@RequestMapping("/t-user")
public class TUserController {

    @Resource
    private ITUserService service;
    @GetMapping("login")
    public Result login(String username,String password){

        return new Result(service.login(username,password));
    }
    @GetMapping
    public Result queryall(TUser user, Page page){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("username",user.getUsername());
        queryWrapper.like("sex",user.getSex());
        return new Result(service.page(page,queryWrapper));
    }
    @DeleteMapping("{id}")
    public Result delbyId(@PathVariable Integer id){
        System.out.println("要删除的id"+id);
        return new Result(service.removeById(id));
    }
    @PostMapping
    public Result SaveAndUpdata(TUser user){
       SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        String birthday = user.getBirthday().substring(0,10)+" 00:00:00";
       user.setBirthday(birthday);
        System.out.println(user.getBirthday());
        boolean b = service.saveOrUpdate(user);
        return new Result(b);
    }
    @GetMapping("chackname")
    public Result Chackname(String username){
        Map<String,Object> map=new HashMap<>();
        map.put("username",username);
        List<TUser> tUsers = service.listByMap(map);
        if (tUsers.size()>0){
            return new Result(true);
        }
        return new Result(false);
    }
}

